{# SPDX-FileCopyrightText: © Fossology contributors
 
   SPDX-License-Identifier: GPL-2.0-only
#}

<script language="javascript">
  function ChangeFilter(selectObj, upload, item) {
    var selectidx = selectObj.selectedIndex;
    var filter = selectObj.options[selectidx].value;
    window.location.assign("?mod={{ name }}&upload=" + upload + "&item=" + item + "&filter=" + filter);
  }

  function deleteHashDecision(hash, upload, type) {
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: '?mod=ajax-copyright-hist&action=deleteHashDecision&type=' + type,
      data: {
        'hash': hash,
        'upload': upload
      },
      success: function(data) {
        $("#deleteHashDecision" + type + hash).hide();
        $("#undoDeleteHashDecision" + type + hash).attr("style","display:inline !important;");
      },
      error: function() { alert('error'); }
    });
  }

  function undoHashDecision(hash, upload, type) {
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: '?mod=ajax-copyright-hist&action=undoHashDecision&type=' + type,
      data: {
        'hash': hash,
        'upload': upload
      },
      success: function(data) {
        $("#undoDeleteHashDecision" + type + hash).hide();
        $("#deleteHashDecision" + type + hash).attr("style","display:inline !important;");
      },
      error: function() { alert('error'); }
    });
  }

  function advanceSearchToRegex(search) {
    var val = $.fn.dataTable.util.escapeRegex(search.trim());
    return val.replace(/\\\(\\\*\\\)/g, "(.*)");
  }

  function testReplacement(table, type) {
    var checkBoxes = $("input:checkbox[class=deleteBySelect" + type + "]:checked");
    var toVal = $('#replaceText' + type).val();
    if (checkBoxes.length == 0 || /^\s*$/.test(toVal)) {
      return;
    }
    var idPrefix = '#testVal' + type;

    var firstRow = checkBoxes.first();
    var testVal = table.api().row($(firstRow).parent().parent()[0]).data()[1];
    testVal = $("<textarea/>").html(testVal).text();  // Decode HTML &lt; &gt;
    var regex = advanceSearchToRegex($('#advSearchText' + type).val());
    var finalVal = toVal;
    if (!/^\s*$/.test(regex)) {
      finalVal = testVal.replace(new RegExp(regex, "gi"), toVal);
    }

    $(idPrefix + 'From').text(testVal);
    $(idPrefix + 'To').text(finalVal);
    $(idPrefix).show();
  }

  function createReplacementText(type) {
    var searchRegex = $('#advSearchText' + type).val();
    var i = 1;
    var replaceText = searchRegex.replace(/\(\*\)/g, function(match) {
      return "$" + (i++);
    });
    $('#replaceText' + type).val(replaceText);
  }

  $(document).ready(function() {
    $('img').tooltip();
  });

  {{ scriptBlock }}
</script>
